package com.beiing.tianshuai.tianshuai.dongtai;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.dongtai.presenter.DynamicPresenter;
import com.beiing.tianshuai.tianshuai.entity.DynamicBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentAndReplyBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2018/1/14 0014.
 */

class DynamicCommentSendClickListener implements View.OnClickListener {

    /**
     * 当前动态条目的位置
     */
    private int dynamicPosition;

    /**
     * presenter
     */
    private DynamicPresenter mPresenter;

    /**
     * 动态数据源
     */
    private DynamicBean.DataBean.DynamicsBean mDynamicBean;

    /**
     * 评论数据源
     */
    private DynamicCommentAndReplyBean mDynamicCommentAndReplyBean;

    /**
     * 输入框
     */
    private EditText mInputEditTextView;

    /**
     * 评论输入框的父容器
     */
    private LinearLayout llCommentInputViewContainer;

    /**
     * context
     */
    private Context mContext;

    /**
     * 出发点击事件所在条目位置
     */
    private int position;

    /**
     * 评论类型或回复类型
     * 0 - 源动态的评论
     * 1 - 转发动态的评论
     * 2 - 源动态的回复
     * 3 - 转发动态的回复
     */
    private int type;

    DynamicCommentSendClickListener(DynamicPresenter presenter,
                                    DynamicCommentAndReplyBean dynamicCommentAndReplyBean,
                                    EditText inputEditTextView, Context context,
                                    int position, int type, LinearLayout llComment, DynamicBean.DataBean.DynamicsBean bean, int dynamicPosition) {
        this.mPresenter = presenter;
        this.mDynamicCommentAndReplyBean = dynamicCommentAndReplyBean;
        this.mInputEditTextView = inputEditTextView;
        this.mContext = context;
        this.position = position;
        this.type = type;
        this.llCommentInputViewContainer = llComment;
        this.mDynamicBean = bean;
        this.dynamicPosition = dynamicPosition;
    }

    DynamicCommentSendClickListener(DynamicPresenter presenter,
                                    DynamicBean.DataBean.DynamicsBean dynamicBean,
                                    EditText inputEditTextView, Context context,
                                    int position, int type,LinearLayout llComment) {
        this.mPresenter = presenter;
        this.mDynamicBean = dynamicBean;
        this.mInputEditTextView = inputEditTextView;
        this.mContext = context;
        this.position = position;
        this.type = type;
        this.llCommentInputViewContainer = llComment;
    }

    @Override
    public void onClick(View v) {
        String content = mInputEditTextView.getText().toString().trim();
        String contentUTF8 = null;
        try {
            contentUTF8 = URLEncoder.encode(content, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 如果评论框中的内容为null则弹出提示
        if (TextUtils.isEmpty(contentUTF8) && mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showShortText("请输入内容哦~");
            return;
        }

        String uid = UserInfoBean.getUid(mContext);
        switch (type) {
            case 0:
                mPresenter.addComment(
                        uid,
                        mDynamicBean.getUid(),
                        mDynamicBean.getIdOne(),
                        contentUTF8,
                        mDynamicBean.getUid(),
                        System.currentTimeMillis() / 1000 + "",
                        position,
                        mDynamicBean
                );
                break;
            case 1:
                mPresenter.addRelayComment(
                        mDynamicBean.getIdOne(),
                        uid,
                        mDynamicBean.getId(),
                        mDynamicBean.getFormUid(),
                        contentUTF8,
                        position,
                        mDynamicBean
                );
                break;
            case 2:
                mPresenter.addReply(
                        mDynamicBean.getIdOne(),
                        mDynamicCommentAndReplyBean.getId(),
                        uid,
                        mDynamicCommentAndReplyBean.getUid(),
                        contentUTF8,
                        mDynamicCommentAndReplyBean.getUid(),
                        position,
                        dynamicPosition,
                        mDynamicBean,
                        mDynamicCommentAndReplyBean
                );
                break;
            case 3:
                mPresenter.addRelayReply(
                        mDynamicBean.getIdOne(),
                        mDynamicCommentAndReplyBean.getId(),
                        uid,
                        mDynamicCommentAndReplyBean.getUid(),
                        contentUTF8,
                        mDynamicCommentAndReplyBean.getUid(),
                        position,
                        dynamicPosition,
                        mDynamicBean,
                        mDynamicCommentAndReplyBean
                );
                break;
        }

        // 点击发送按钮结束后清空输入框的内容并隐藏 收起软键盘
        mInputEditTextView.setText("");
        llCommentInputViewContainer.setVisibility(View.GONE);
        InputMethodManager inputMethodManager = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.hideSoftInputFromWindow
                    (mInputEditTextView.getWindowToken(), 0);
        }
    }
}
